
package com.vinaprime.primeschedule.databaselayer;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.vinaprime.primeschedule.entities.VPGroupHeader;
import com.vinaprime.primeschedule.entities.VPGroupLine;

/** * 
 * @author LONGPN
 * @since 17/06/2013
 *
 */
@SuppressLint("NewApi")
public class VPGroupHeader_DL {
	//Khai bao cau truc cua table, tuong duong voi cac table o database
	private static final String DB_TABLE_NAME = "VP_GROUP_HEADER";
	private static final String DB_TABLE_VPGH_ID = "VPGH_ID";
	private static final String DB_TABLE_VPGH_CODE= "VPGH_CODE";
	private static final String DB_TABLE_VPGH_NAME= "VPGH_NAME";
	private static final String DB_TABLE_VPGH_DESCRIPTION = "VPGH_DESCRIPTION";
	private static final String DB_TABLE_VPGH_ENABLE_FLAG  ="VPGH_ENABLE_FLAG";
	public DatabaseController_DL myDatabaseControllerDL;
	public VPGroupHeader_DL (Context context, String path)
	{
		this.myDatabaseControllerDL = new DatabaseController_DL(context, path);
	}
	//Get, seter 
	public DatabaseController_DL getMyDatabaseControllerDL() {
		return myDatabaseControllerDL;
	}
	public void setMyDatabaseControllerDL(
			DatabaseController_DL myDatabaseControllerDL) {
		this.myDatabaseControllerDL = myDatabaseControllerDL;
	}
	public  static String getDbTableName() {
		return DB_TABLE_NAME;
	}
	public static String getDbVpghId() {
		return DB_TABLE_VPGH_ID;
	}
	public  static String getDbVpghCode() {
		return DB_TABLE_VPGH_CODE;
	}
	public  static String getDbVpghName() {
		return DB_TABLE_VPGH_NAME;
	}
	public static String getDbVpghDescription() {
		return DB_TABLE_VPGH_DESCRIPTION;
	}
	public static String getDbVpghEnableFlag() {
		return DB_TABLE_VPGH_ENABLE_FLAG;
	}
	//Cac phuong thuc lien quan
	public Cursor getAllGroupHeader()
	{
		Cursor cur = null;
		SQLiteDatabase  db = this.myDatabaseControllerDL.getWritableDatabase();
		String strSelect = "SELECT * FROM "+ DB_TABLE_NAME;
		cur = db.rawQuery(strSelect, null);		
		return cur;
		
	}
	
	public Cursor getNewGroupHeaderID()
	{
		Cursor cur  = null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT MAX ("+DB_TABLE_VPGH_ID+") AS " +DB_TABLE_VPGH_ID+ " FROM "+ DB_TABLE_NAME;
		cur  = db.rawQuery(strQuery, null);
		return cur;
	}
	public Cursor getGroupHeaderbyId(int Id)
	{
		Cursor cur =  null;
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPGH_ID +" = " + Id ;
		cur = db.rawQuery(strQuery, null);
		//SQLiteDatabase db = this.myDatabaseControllerDL.getReadableDatabase();
		//String strQuery = "SELECT * FROM "+ DB_TABLE_NAME + " WHERE "+ DB_TABLE_VPC_ID +" = ?" ;
		//cur = db.rawQuery(strQuery, new String[]{String.valueOf(Id)});
		return cur;
	}
	public void AddGroupHeader (VPGroupHeader _groupHeader)	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPGH_ID, _groupHeader.getVpghId());
		values.put(DB_TABLE_VPGH_CODE, _groupHeader.getVpghCode());
		values.put(DB_TABLE_VPGH_NAME, _groupHeader.getVpghName());
		values.put(DB_TABLE_VPGH_DESCRIPTION, _groupHeader.getVpghDescription());
		values.put(DB_TABLE_VPGH_ENABLE_FLAG,  _groupHeader.getVpghEnableFlag());
		db.insert(DB_TABLE_NAME, null, values);		
		db.close();
	}
	public int updateGroupHeader (VPGroupHeader _groupHeader)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL .getWritableDatabase();
		ContentValues values = new ContentValues();
		values.put(DB_TABLE_VPGH_ID, _groupHeader.getVpghId());
		values.put(DB_TABLE_VPGH_CODE, _groupHeader.getVpghCode());
		values.put(DB_TABLE_VPGH_NAME, _groupHeader.getVpghName());
		values.put(DB_TABLE_VPGH_DESCRIPTION, _groupHeader.getVpghDescription());
		values.put(DB_TABLE_VPGH_ENABLE_FLAG, _groupHeader.getVpghEnableFlag());
		return db.update(DB_TABLE_NAME, values, DB_TABLE_VPGH_ID + "= ?", new String[] {String.valueOf(_groupHeader.getVpghId())});
		
	}
	
	public void deleteGroupHeader (VPGroupHeader _groupHeader)
	{
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPGH_ID + "= ?", new String[] { String.valueOf(_groupHeader.getVpghId()) });
		
		db.close();
	}
	public void deleteGroupHeaderbyID(int _VpghID){
		SQLiteDatabase db = this.myDatabaseControllerDL.getWritableDatabase();
		db.delete(DB_TABLE_NAME, DB_TABLE_VPGH_ID + "= ?",new String[]{ String.valueOf(_VpghID)});
		db.close();
	}

	
}
